package pcjavanet.mobilesell.service;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;  
import java.util.Iterator; 
import java.util.Map;      
import pcjavanet.mobilesell.model.Catalog;
import pcjavanet.mobilesell.model.CatalogExample;
import pcjavanet.mobilesell.model.Catalog; 
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.CatalogExample;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.QryTerm;
import pcjavanet.mobilesell.persistence.CatalogMapper;
import pcjavanet.mobilesell.persistence.CatalogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class CatalogService {


  @Autowired
  private  CatalogMapper catalogMapper;


  public int  insert( Catalog catalog ) throws Exception{
	return catalogMapper.insert (catalog);
  }


  public Catalog  selectByPrimaryKey( Catalog  bean ) {
 	int dbid = bean.getDbid();
	Catalog catalog = catalogMapper.selectByPrimaryKey( dbid );
	Catalog catalog0 = catalogMapper.selectByPrimaryKey( catalog.getRefCatalogDbid() );
	catalog.setCatalog(catalog0) ;
	return catalog ;
  }


  public int  updateByPrimaryKeySelective( Catalog catalog ) throws Exception{
	return catalogMapper.updateByPrimaryKeySelective( catalog );
  }


  public int  deleteByPrimaryKey( Catalog  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return catalogMapper.deleteByPrimaryKey( dbid );
  }


  public List<Catalog>  selectByExample( Catalog catalog ) throws Exception{
    CatalogExample example = new CatalogExample();
	return catalogMapper.selectByExample( example );
  }

  public List<Catalog>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    CatalogExample example = new CatalogExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<Catalog> ls=catalogMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> catalogIdmap = new HashMap<Integer,Integer>();
  		for(Catalog  m : ls){
  			m.setCatalog(new Catalog ()) ; 
  			if ( m.getRefCatalogDbid() != null ) 
  				catalogIdmap.put(m.getRefCatalogDbid(), m.getRefCatalogDbid());
  		};
  		List<Integer> refCatalogDbids = new ArrayList<Integer>();
  		Iterator<Integer> refCatalogDbidit = catalogIdmap.keySet().iterator(); 
  		while ( refCatalogDbidit.hasNext() ){
  			refCatalogDbids.add( refCatalogDbidit.next() );
  		}
  		CatalogExample catalogExample = new CatalogExample();
  		if ( refCatalogDbids.size() > 0 ) 
  			catalogExample.createCriteria().andDbidIn( refCatalogDbids );
  		List<Catalog> catalogs = catalogMapper.selectByExample( catalogExample);
  		for(Catalog m : ls ){
  			for(Catalog c : catalogs ){
  				if ( m.getRefCatalogDbid() !=null && c.getDbid() !=null && m.getRefCatalogDbid().intValue() == c.getDbid().intValue() ) {
  					m.setCatalog( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    CatalogExample example = new CatalogExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return catalogMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( Catalog catalog , PageBean pageBean ){
    CatalogExample example = new CatalogExample();
	return catalogMapper.countByExample( example );
  }
  public List<Catalog> selectByExamplePage( Catalog catalog , PageBean pageBean ){
  CatalogExample example = new CatalogExample();
	return catalogMapper.selectByExamplePage( example , pageBean );
  }

}
